// 短信发送记录
import { DataTypes, UUIDV4 } from 'sequelize';
import { MySqlModel, getSequelize } from '../db';

class SmsLog extends MySqlModel {

}

SmsLog.init({
  id: {
    type: DataTypes.UUID, unique: 'id', allowNull: false, defaultValue: UUIDV4, validate: { isUUID: 4 }, primaryKey: true, comment: '主键'
  },
  hotel_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '主体'
  },
  sequence: {
    type: DataTypes.STRING(50), allowNull: false, comment: '序列号'
  },
  mobile: {
    type: DataTypes.STRING(50), allowNull: true, defaultValue: null, comment: '手机号'
  },
  confirm_number: {
    type: DataTypes.STRING(20), allowNull: false, comment: '确认号'
  },
  url_id: {
    type: DataTypes.BIGINT, allowNull: true, comment: 'mini_url id'
  },
  sign_name: {
    type: DataTypes.STRING(20), allowNull: false, comment: '签名'
  },
  sms_code: {
    type: DataTypes.STRING(50), allowNull: false, comment: '短信模板'
  },
  content: {
    type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: '短信内容'
  },
  template_param: {
    type: DataTypes.STRING(200), allowNull: false, comment: '短信参数'
  },
  status: {
    type: DataTypes.STRING(20), allowNull: true, defaultValue: 'INIT', validate: { isIn: { msg: '无效的状态值', args: [['INIT', 'SUCCESS', 'FAILED']] } }, comment: '发送状态'
  },
  result: {
    type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: '执行结果'
  },
  user_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '操作员'
  },
  order_by: {
    type: DataTypes.INTEGER, allowNull: false, comment: '排序'
  },
  created_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '更新时间'
  }
}, {
  sequelize: getSequelize(),
  tableName: 'sms_log',
  comment: '短信发送记录',
  indexes: [
    { fields: ['hotel_id', 'sequence'] }
  ]
});

SmsLog.associate = (models) => {
  SmsLog.belongsTo(models.Users, {
    foreignKey: 'user_id',
    targetKey: 'id',
    as: 'user',
    constraints: false
  });
};

export default SmsLog;
